﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Medical.Consts;
using Sector7.Framework.Utility;
using System.ComponentModel;
using Sector7.Medical.Biz;
using Sector7.Medical.EntityType;

namespace Sector7.Medical.STOM.Windows
{
    /// <summary>
    /// AddPurchasesBillItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddPurchasesBillItemWindow : AddModeWindowBase
    {
        #region Accessor
        /// <summary>
        /// 获取或设置采购项
        /// </summary>
        public M7_PurchasesBillItem BillItem
        {
            get { return m_entity as M7_PurchasesBillItem; }
            set { m_entity = value; }
        }
        #endregion

        #region Public functions
        #endregion

        #region Private functions
        protected override void InitializeForCreating()
        {
            txbPurchasesPrice.Text = string.Empty;
            txbSalesPrice.Text = string.Empty;
            txbNumber.Text = string.Empty;
            txbBatchNumber.Text = string.Empty;
            txbLotNumber.Text = string.Empty;
            dtpProduced.SelectedDate = DateTime.Now;
            dtpAvailable.SelectedDate = dtpProduced.SelectedDate;

            m_entity = new M7_PurchasesBillItem();
            GCUtil.Collect();

            base.InitializeForCreating();
        }

        protected override void InitializeForUpdating()
        {
            base.InitializeForUpdating();
        }

        protected override void InitializeForOthers()
        {
            base.InitializeForOthers();
        }

        private void LoadData()
        {
            ClientUtil.LoadItemsSource(cbbStockUnit, m_stockUnitList);

            Senvironments.ProgressWindow.Visibility = Visibility.Hidden;
        }

        private void CheckUnitIsExist()
        {
            if (cbbStockUnit.SelectedItem == null && cbbStockUnit.Text != null)
            {
                AppendNewUnitToComboBox(cbbStockUnit, cbbStockUnit.Text, C_UnitFlags.STOCK_UNIT);
            }
        }

        private void AppendNewUnitToComboBox(ComboBox comboBox, string unitName, int flags)
        {
            var unit = new M7_Unit();

            unit.UnitName = unitName;
            unit.Flags = flags;
            unit.UnitId = Agent_INFS.AddUnit(unit);

            List<M7_Unit> unitList = null;
            switch (flags)
            {
                case C_UnitFlags.STOCK_UNIT:
                    unitList = m_stockUnitList;
                    break;
                default:
                    break;
            }

            if (unitList != null)
            {
                unitList.Add(unit);
                ClientUtil.LoadItemsSource(comboBox, unitList);
                comboBox.SelectedItem = unit;
            }
        }

        protected override bool CheckData()
        {
            CheckUnitIsExist();

            m_errors.Clear();

            //if (RegularUtil.IsDecimal(txbPurchasesPrice.Text))
            //{
            //    BillItem.PurchasesPrice = Convert.ToDecimal(txbPurchasesPrice.Text);
            //}
            //else
            //{
            //    m_errors.Add("采购单价", "必须为小数！");
            //}
            //if (RegularUtil.IsDecimal(txbSalesPrice.Text))
            //{
            //    BillItem.SalesPrice = Convert.ToDecimal(txbSalesPrice.Text);
            //}
            //else
            //{
            //    m_errors.Add("销售单价", "必须为小数！");
            //}
            //if (RegularUtil.IsNumber(txbNumber.Text) || RegularUtil.IsDecimal(txbNumber.Text))
            //{
            //    BillItem.Number = Convert.ToDecimal(txbNumber.Text);
            //}
            //else
            //{
            //    m_errors.Add("数量", "必须为有效数字！");
            //}            
            BillItem.PurchasesPrice = Convert.ToDecimal(txbPurchasesPrice.Text);
            BillItem.SalesPrice = Convert.ToDecimal(txbSalesPrice.Text);
            BillItem.Number = Convert.ToDecimal(txbNumber.Text);

            return base.CheckData();
        }

        protected override void AddData()
        {
            //BillItem.StockUnitId = Convert.ToInt32(cbbStockUnit.SelectedValue);
            //BillItem.BatchNumber = txbBatchNumber.Text;
            //BillItem.LotNumber = Convert.ToInt32(txbLotNumber.Text);
            //BillItem.ProducedDate = (DateTime)dtpProduced.SelectedDate;
            //BillItem.AvailableDate = (DateTime)dtpAvailable.SelectedDate;
            //BillItem.StockUnit = cbbStockUnit.SelectedItem as M7_Unit;
            //BillItem.Drug = lsbMedicinal.SelectedItem as M7_Drug;
            //BillItem.DrugId = BillItem.Drug.DrugId;
            //BillItem.StockSpec = Convert.ToDecimal(txbStockSpec.Text);
            //BillItem.PackUnitId = BillItem.Drug.PackUnitId;
            //BillItem.PackUnit = BillItem.Drug.PackUnit;

            base.AddData();
        }

        private void AddModeWindowBase_Loaded(object sender, RoutedEventArgs e)
        {
            Senvironments.ProgressWindow.Visibility = Visibility.Visible;

            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            worker.RunWorkerAsync();
        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            m_stockUnitList = Agent_INFS.GetUnitListByFlags(C_UnitFlags.STOCK_UNIT);

            this.Dispatcher.BeginInvoke(new DelegateWithNothing(LoadData));
        }

        private void SearchBox_KeywordsChanging(object sender, KeyEventArgs e)
        {
            ClientUtil.LoadItemsSource(lsbMedicinal, Agent_INFS.GetDrugListByKeywords(searchBox.Keywords));

            lsbMedicinal.Focus();
        }

        private void SearchBox_KeywordsReset(object sender, EventArgs e)
        {
            ClientUtil.LoadItemsSource(lsbMedicinal, Agent_INFS.ResetDrugList());
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var window1 = ReflectionUtil.CreateInstance(
                @"AssetsManagement.dll",
                "Sector7.Medical.ASSM.Windows.Window1") as AddModeWindowBase;
            window1.InitializeWindow(Framework.Enums.CrudType.Create);
            window1.ShowDialog();
        }

        private void lsbMedicinal_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedDrug = lsbMedicinal.SelectedItem as M7_Drug;
            grdDetails.DataContext = selectedDrug;
            //txbPackUnit.Text = selectedDrug.PackUnit.UnitName;
        }
        #endregion

        #region Structure
        public AddPurchasesBillItemWindow()
        {
            InitializeComponent();

            m_enableShortcut = false;
        }
        #endregion

        #region Inner members
        private List<M7_Unit> m_stockUnitList = null;
        #endregion
    }
}
